package com.aischool.api;

import java.util.List;

import com.aischool.routes.RouteMapping;
import com.aischool.service.SysAppService;
import com.aischool.service.SysMenuService;
import com.aischool.service.SysPermissionService;
import com.aischool.service.SysRoleService;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

@RouteMapping(url = "/api/permission")
public class PermissionController extends Controller {

	@Inject
	SysAppService sysAppService;
	@Inject
	SysMenuService sysMenuService;
	@Inject
	SysRoleService sysRoleService;
	@Inject
	SysPermissionService sysPermissionService;
	
	public void getInit() {
		// 结果类
		JsonResult jr = new JsonResult();
		
		List<Record> listApp = sysAppService.getSysAppAll();
		jr.setDicKV("listapp", listApp);
		
		Object obj = sysRoleService.getSysRoleAll();
		jr.setDicKV("listrole", obj);
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	public void getPower() {
		// 结果类
		JsonResult jr = new JsonResult();
		int app_id = getInt("so_app_id", 0);
		int role_id = getInt("role_id", 0);
		
		if (app_id > 0 && role_id>0) {
			Object obj = sysMenuService.getAppMenu(app_id, 1);
			jr.setDicKV("listmenu", obj);
			obj = sysPermissionService.getAppRoleMenu(app_id, role_id);
			jr.setDicKV("listpower", obj);
		}
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	public void getMyMenu() {
		// 结果类
		JsonResult jr = new JsonResult();
		int user_id = this.getAttrForInt("login_user_id");
		int app_id = this.getAttrForInt("login_app_id");
		
		if (app_id > 0 && user_id>0) {
			Object obj = sysPermissionService.getAppUserMenu(app_id, user_id);
			jr.setDicKV("listmenu", obj);
		}
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
	}
	
	public void save() {
		// 结果类
		JsonResult jr = new JsonResult();
		int app_id = getInt("app_id", 0);
		int role_id = getInt("role_id", 0);
		String ids = getPara("ids", "");
		
		if (app_id <= 0 || role_id <= 0) {
			jr.setCode(400);
			jr.setMsg("缺少参数！");
		}else {
			boolean bf = sysPermissionService.updatePower(app_id, role_id, ids);
			if (bf) {
				jr.setEnum(JsonCodeEnum.Success);
			}else {
				jr.setEnum(JsonCodeEnum.Faild);
			}
		}
		
		
		renderJson(jr.Dic);
		
	}
}
